package uml.GUI;

import java.awt.Color;
import java.awt.Component;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;

import uml.eventsystem.IObserver;
import uml.eventsystem.ISubject;
import uml.eventsystem.SubjectImpl;
import uml.eventsystem.events.UMLEvent;
import uml.eventsystem.events.UMLEventId;
import uml.util.UMLUtility;

public class UMLObjectButton extends JButton implements MouseListener, ISubject
{
	private UMLEventId m_eventID;
	private SubjectImpl m_subjectImpl = new SubjectImpl(this);
	/**
	 * Sets up a button with text of name
	 * @param name
	 * @param event
	 */
	public UMLObjectButton(String name, UMLEventId event)
	{
		super(name);
		m_eventID = event;
	}
	public UMLObjectButton(Component component, UMLEventId event)
	{
		UMLUtility.removeMouseAndKeyListeners(component);
		Object children[] = UMLUtility.getChildren(component);
		for(Object child : children)
			if(child instanceof Component)
			{
				UMLUtility.removeMouseAndKeyListeners((Component)child);
				((Component)child).addMouseListener(this);
				((Component)child).setFocusable(false);
			}
		this.add(component);
		m_eventID = event;
		this.setBorder(null);
	}
	/**
	 * returns the buttons <tt>UMLEventID</tt>
	 * @return
	 */
	public UMLEventId getUMLEventId()
	{
		return m_eventID;
	}
	@Override
	public void mouseClicked(MouseEvent event) 
	{
		notifyObservers(m_eventID);
	}
	@Override
	public void mouseEntered(MouseEvent event) 
	{
		this.setBorder(BorderFactory.createLineBorder(Color.blue));
	}
	@Override
	public void mouseExited(MouseEvent event) 
	{
		this.setBorder(BorderFactory.createLineBorder(Color.green));
	}
	@Override
	public void mousePressed(MouseEvent event) 
	{
		
	}
	@Override
	public void mouseReleased(MouseEvent event)
	{
		
	}
	@Override
	public void addObserver(IObserver observer, UMLEventId eventId) 
	{
		m_subjectImpl.addObserver(observer, eventId);
	}
	@Override
	public void removeObserver(IObserver observer, UMLEventId eventId) 
	{
		m_subjectImpl.removeObserver(observer, eventId);
	}
	@Override
	public void notifyObservers(UMLEventId eventId) 
	{
		m_subjectImpl.notifyObservers(new UMLEvent(eventId, this));
	}
}
